
import Vue from 'vue';
import VueRouter from 'vue-router';
import App from './components/app.vue';
import header from './components/header.vue'
import footer from './components/footer.vue'
import Course from './components/course.vue'
import Js from './components/js.vue'
import Vuejs from './components/vuejs.vue'
import Login from './components/login.vue'
import User from './components/user.vue'
//注册全局组件
Vue.component('headerVue', header);
Vue.component('footerVue', footer);
//安装插件
Vue.use(VueRouter); //挂载属性
//创建路由对象并配置路由规则
let router = new VueRouter({
    //routes
    routes: [{
            path: '/',
            redirect: { name: 'course' },
        },
        {
            path: '/login',
            name:'login',
           component: Login,
        },
           {
            path: '/user',
            name:'user',
            meta:{
            login_required:true
            },
			component: User,
        },
        {
            name: 'course',
            path: '/course',
            component: Course,
            children: [
                //-> 这里很灵活，如果你写上/xxx  就是绝对路径， /oumei
                //如果你不写/  ,那么就是相对路径 /music/oumei
                { name: 'course.js', path: 'js', component: Js },
                //标识一下，当前路由之间的关系，格式不是必须的
                { name: 'course.vue', path: 'vue', component: Vuejs }
            ]
        }

    ]
});

router.beforeEach(function(to,from,next){
    var logged_in=false;
    //var logged_in=true;
//  if(!logged_in && to.path=='/user'){
//    next('/login')	
//  }     
//  else  {
//  	next();
//  }	
    if(!logged_in && to.matched.some(function(item){
            return item.meta.login_required
        })){
        	next('/login');
        }
        
    else{
    	  next();
    }
      
});
new Vue({
    el: '#app',
    router, //简写router
    render: c => c(App),
})